using System;
using System.Linq;
using JNPF.Utils;
using System.Threading.Tasks;
using JNPF.Data;
using JNPF.Data.Util;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using JNPF.Basics.Models;
using System.Data.Common;
using System.Text;
using Castle.Core.Resource;
using Senparc.Weixin.MP.AdvancedAPIs;

namespace JNPF.Basics.Logic
{
    public class CustomerneedService : DataFactory, CustomerneedIService
    {
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<CustomerneedEntity> GetInfo(int id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<CustomerneedEntity>(m => m.Id == id);
            }
        }



        /// <summary>
        /// 根据流水号 获取 信息
        /// </summary>
        /// <param name="serialid"></param>
        /// <returns></returns>
        public async Task<CustomerneedEntity> GetInfoBySerialid(string serialid) 
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<CustomerneedEntity>(m => m.Serialid == serialid);
            }
        }

        /// <summary>
        /// 列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<CustomerneedListOutput>> GetPageList(CustomerneedListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var parameters = new List<DbParameter>();
                var dbSql = new StringBuilder();
                dbSql.Append(@"SELECT
	                            need.F_ID,
	                            need.F_Type,
	                            need.F_CustomerId,
	                            need.F_UserId,
	                            need.F_UserName,
	                            need.F_NeedDesc,
	                            need.F_NeedAmount,
	                            need.F_needDate,
	                            need.F_SortCode,
	                            need.F_EnabledMark,
	                            need.F_CreatorTime,
	                            need.F_CreatorUser,
	                            need.F_CreatorUserId,
	                            need.F_LastModifyTime,
	                            need.F_LastModifyUserId,
	                            need.F_DeleteMark,
	                            need.F_DeleteTime,
	                            need.F_DeleteUserId,
	                            need.F_BusinessId,
	                            cus.F_CustomerName,
                                cus.F_HideName 
                            FROM
	                            jiushi_customerneed need
	                            LEFT JOIN jiushi_customerbase cus ON need.F_CustomerId = cus.F_ID
	                            where 1=1 and need.F_DeleteMark = 0");

                if (input.type != null && input.type >= 0)
                {
                    dbSql.Append("  and need.F_Type = @type");
                    parameters.Add(DataHelper.CreateParameter("@type", input.type));

                }



                input.sidx = "F_CreatorTime";
                return await PageHelper.GetPageResultAsync<CustomerneedListOutput>(db, input, dbSql.ToString(), parameters.ToArray());

            }

        }

        /// <summary>
        /// 企业用户  ===  手机号码对应起来的 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<CustomerneedListOutput>> GetBusPageList(CustomerneedListQueryInput input)
        {

            UserInfo userInfo = UserProvider.Instance.Get();
            input.sidx = "F_CreatorTime";
            using (var db = this.Basic)
            {
                var parameters = new List<DbParameter>();
                var dbSql = new StringBuilder();
                dbSql.Append(@"SELECT
	                            need.F_ID,
	                            need.F_Type,
	                            need.F_CustomerId,
	                            need.F_UserId,
	                            need.F_UserName,
	                            need.F_NeedDesc,
	                            need.F_NeedAmount,
	                            need.F_needDate,
	                            need.F_SortCode,
	                            need.F_EnabledMark,
	                            need.F_CreatorTime,
	                            need.F_CreatorUser,
	                            need.F_CreatorUserId,
	                            need.F_LastModifyTime,
	                            need.F_LastModifyUserId,
	                            need.F_DeleteMark,
	                            need.F_DeleteTime,
	                            need.F_DeleteUserId,
	                            need.F_BusinessId,
	                            cus.F_CustomerName 
                            FROM
	                            jiushi_customerneed need
	                            LEFT JOIN jiushi_customerbase cus ON need.F_CustomerId = cus.F_ID
	                            where 1=1 ");
                dbSql.Append("  and cus.F_MobilePhone = @mobilePhone");
                parameters.Add(DataHelper.CreateParameter("@mobilePhone", userInfo.UserAccount));

                if (input.type != null && input.type >= 0)
                {
                    dbSql.Append("  and need.F_Type = @type");
                    parameters.Add(DataHelper.CreateParameter("@type", input.type));

                }
                return await PageHelper.GetPageResultAsync<CustomerneedListOutput>(db, input, dbSql.ToString(), parameters.ToArray());
            }

        }

        /// <summary>
        /// 客户经理列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<CustomerneedListOutput>> GetCuseBusList(CustomerneedListQueryInput input)
        {

            UserInfo userInfo = UserProvider.Instance.Get();
            input.sidx = "F_CreatorTime";
            using (var db = this.Basic)
            {
                var parameters = new List<DbParameter>();
                var dbSql = new StringBuilder();
                dbSql.Append(@"SELECT
	                            need.F_ID,
	                            need.F_Type,
	                            need.F_CustomerId,
	                            need.F_UserId,
	                            need.F_UserName,
	                            need.F_NeedDesc,
	                            need.F_NeedAmount,
	                            need.F_needDate,
	                            need.F_SortCode,
	                            need.F_EnabledMark,
	                            need.F_CreatorTime,
	                            need.F_CreatorUser,
	                            need.F_CreatorUserId,
	                            need.F_LastModifyTime,
	                            need.F_LastModifyUserId,
	                            need.F_DeleteMark,
	                            need.F_DeleteTime,
	                            need.F_DeleteUserId,
	                            need.F_BusinessId,
	                            cus.F_CustomerName  
                            FROM
	                            jiushi_customerneed need
	                            LEFT JOIN jiushi_customerbase cus ON need.F_CustomerId = cus.F_ID
							    left join jiushi_customerneedrecord recored on recored.F_CustomerNeedId = need.F_ID
	                            where 1=1 ");

                dbSql.Append(" and  recored.F_CreatorUserId  = @UserId");
                parameters.Add(DataHelper.CreateParameter("@UserId", userInfo.UserId));


                if (input.type != null && input.type >= 0)
                {
                    dbSql.Append("  and need.F_Type = @type");
                    parameters.Add(DataHelper.CreateParameter("@type", input.type));

                }


                return await PageHelper.GetPageResultAsync<CustomerneedListOutput>(db, input, dbSql.ToString(), parameters.ToArray());
            }

        }


        /// <summary>
        /// 信贷管家   列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<CustomerneedListOutput>> GetPagCuseList(CustomerneedListQueryInput input)
        {

            UserInfo userInfo = UserProvider.Instance.Get();
            input.sidx = "F_CreatorTime";
            using (var db = this.Basic)
            {
                var parameters = new List<DbParameter>();
                var dbSql = new StringBuilder();
                dbSql.Append(@"SELECT
	need.F_ID,
	need.F_Type,
	need.F_CustomerId,
	need.F_UserId,
	need.F_UserName,
	need.F_NeedDesc,
	need.F_NeedAmount,
	need.F_needDate,
	need.F_SortCode,
	need.F_EnabledMark,
	need.F_CreatorTime,
	need.F_CreatorUser,
	need.F_CreatorUserId,
	need.F_LastModifyTime,
	need.F_LastModifyUserId,
	need.F_DeleteMark,
	need.F_DeleteTime,
	need.F_DeleteUserId,
	need.F_BusinessId,
	cus.F_CustomerName 
FROM
	jiushi_customerneed need
	INNER JOIN jiushi_customerbase cus ON need.F_CustomerId = cus.F_ID
	INNER join jiushi_customeruser cuser on cuser.F_CustomerId = cus.F_ID
	where 1=1 
 and cuser.F_Type = 1 
	");
                dbSql.Append(" and  cuser.F_UserId = @UserId");
                parameters.Add(DataHelper.CreateParameter("@UserId", userInfo.UserId));

                if (input.type != null && input.type >= 0)
                {
                    dbSql.Append("  and need.F_Type = @type");
                    parameters.Add(DataHelper.CreateParameter("@type", input.type));

                }

                return await PageHelper.GetPageResultAsync<CustomerneedListOutput>(db, input, dbSql.ToString(), parameters.ToArray());
            }

        }





        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(CustomerneedEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(CustomerneedEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(CustomerneedEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<List<CustomerneedEntity>> GetList(CustomerneedListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<CustomerneedEntity>();
                //查询需求描述
                if (!string.IsNullOrEmpty(input.needDesc))
                {
                    queryWhere = queryWhere.And(m => m.NeedDesc.Contains(input.needDesc));
                }

                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<CustomerneedEntity>().Where(queryWhere).ToListAsync();
            }
        }


    }






}